/*
 * Copyright 2012 Gregor Schauer
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package at.schauer.gregor.vienna.datastore.file;

import at.schauer.gregor.vienna.context.ViennaContext;
import at.schauer.gregor.vienna.datastore.StorageMetadata;
import at.schauer.gregor.vienna.exception.ViennaException;
import at.schauer.gregor.vienna.serializer.Serializer;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.util.DefaultPropertiesPersister;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author Gregor Schauer
 */
public class PropertiesFileDataStore<T> extends AbstractFileDataStore<T> {
	public PropertiesFileDataStore(ViennaContext ctx, File rootDirectory) {
		this(ctx, rootDirectory, "properties");
	}

	public PropertiesFileDataStore(ViennaContext ctx, File rootDirectory, String ext) {
		super(ctx, rootDirectory, ext);
	}

	@Override
	public <E extends T> E read(File file, StorageMetadata<E> metadata) {
		if (!file.isFile()) {
			return null;
		}

		Map<String, Object> map;
		try {
			Properties properties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
			map = fromProperties(properties);
			map.remove(Serializer.CLASS);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}

		Serializer<E> serializer = ctx.getSerializer(metadata.type);
		return serializer.load(map, metadata.type);
	}

	@Override
	public void write(File file, StorageMetadata<T> metadata) {
		Serializer<T> serializer = ctx.getSerializer(metadata.type);
		Map<String, Object> map = serializer.save(metadata.object);
		Properties properties = MapUtils.toProperties(map);

		OutputStream writer = null;
		try {
			writer = new FileOutputStream(file);
			DefaultPropertiesPersister persister = new DefaultPropertiesPersister();
			persister.store(properties, writer, null);
		} catch (IOException e) {
			throw new ViennaException(e);
		} finally {
			IOUtils.closeQuietly(writer);
		}
	}

	public static Map<String, Object> fromProperties(Properties properties) {
		Map<String, Object> map = new LinkedHashMap<String, Object>();
		for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements(); ) {
			String key = (String) e.nextElement();
			map.put(key, properties.getProperty(key));
		}
		return map;
	}
}
